//
//  ViewController.m
//  11-26date
//
//  Created by mac on 15-11-26.
//  Copyright (c) 2015年 huangfeng. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<inputFinishDelegate,UITableViewDataSource>
{
    NSMutableArray *date;
    NSMutableArray *tels;
    NSIndexPath *curIndexPath;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"通讯录";
//    date = @[
//             @"傻逼",
//             @"二货",
//             @"蠢蛋"
//             ];
//    tels = @[
//            @"15348248925",
//            @"14862785625",
//            @"18627856334"
//            ];
//    
    NSUserDefaults *df =[NSUserDefaults standardUserDefaults];
//    [df setObject:date forKey:@"date"];
//    [df setObject:tels forKey:@"tels"];
//    [df synchronize];
    
    date = [[df objectForKey:@"date"] mutableCopy];
    tels = [[df objectForKey:@"tels"] mutableCopy];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEditing:)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableView.isEditing ? date.count + 1 : date.count;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row - date.count ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

- (void)onEditing:(UIBarButtonItem *)btn{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    btn.title = self.editing ? @"完成" : @"编辑";
    [self.tableView reloadData];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if (indexPath.row == date.count) {
        return cell;
    }
    
    cell.textLabel.text = date[indexPath.row];
    cell.detailTextLabel.text = tels[indexPath.row];
    return cell;
}



- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        NSMutableArray * arr =date.mutableCopy;
//        [arr removeObjectAtIndex:indexPath.row];
//        date = arr;
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//    }
    
    [date removeObjectAtIndex:indexPath.row];
    [tels removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController *con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    curIndexPath = indexPath;
    con.tel = cell.detailTextLabel.text;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
    UIApplication *app = [UIApplication sharedApplication];
    NSString *tel = [NSString stringWithFormat:@"sms://%@",tels[indexPath.row]];
    [app openURL:[NSURL URLWithString:tel]];
}

- (void)inputFinished:(NSString *)text{
    NSLog(@"indexPath:%@",[self.tableView indexPathForSelectedRow]);
    NSMutableArray *arr = tels.mutableCopy;
    [arr replaceObjectAtIndex:curIndexPath.row withObject:text];
    tels = arr;
    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
